<template>
	<use-box :loading="formLoading">
		<el-form :model="form" ref="ruleForm">
			<h3>VIP参数</h3>
			<el-form-item>
				<div class="dflex_vertical_c margin_t_20">
					<div class="front_txt">一年会员，需要</div>
					<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
						v-model.number="form.vip_fees" type="number" oninput="value = parseInt(value) || 1"></el-input>
					<div class="rear_txt">元。</div>
					<div class="remark_txt margin_t_0 h_initial">一年365天</div>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">会员可享</div>
					<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
						v-model.number="form.vip_integral_mul" type="number" oninput="value = parseInt(value) || 1">
					</el-input>
					<div class="rear_txt">倍积分。</div>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">开通会员预省</div>
					<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
						v-model.number="form.vip_pre_saved_amount" type="number" oninput="value = parseInt(value) || 1">
					</el-input>
					<div class="rear_txt">元。</div>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">非会员描述语</div>
					<el-input :disabled="addBtnLoading" class="input txt" size="small" v-model="form.description"
						clearable></el-input>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">会员描述语</div>
					<el-input :disabled="addBtnLoading" class="input txt" size="small" v-model="form.vip_description"
						clearable></el-input>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">支付标题</div>
					<el-input :disabled="addBtnLoading" class="input txt" size="small" v-model="form.pay_title"
						clearable></el-input>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">活动ID</div>
					<el-input :disabled="addBtnLoading" class="input txt" size="small" v-model="form.entitlement_id"
						clearable></el-input>
				</div>
			</el-form-item>
			<h3>VIP权益说明</h3>
			<el-form-item label-width="120px">
				<use-editor :disabled="addBtnLoading" class="margin_t_20" ref="editorVipDesc"
					@editorChange="editorChangeVipDesc">
				</use-editor>
			</el-form-item>
			<h3>开通说明</h3>
			<el-form-item label-width="120px">
				<use-editor :disabled="addBtnLoading" class="margin_t_20" ref="editorDesc"
					@editorChange="editorChangeDesc"></use-editor>
			</el-form-item>
			<el-form-item label-width="210px">
				<use-affix position="bottom" :offset="58">
					<el-button :loading="addBtnLoading" round icon="el-icon-check" class="confirm-btn"
						@click="submitForm('ruleForm')">提交</el-button>
				</use-affix>
			</el-form-item>
		</el-form>
	</use-box>
</template>

<script>
	const __name = 'wmzmall-member-parameter';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				form: {
					vip_fees: 0,
					vip_integral_mul: 0,
					vip_pre_saved_amount: 0,
					description: '',
					vip_description: '',
					pay_title: '',
					entitlement_id:'',
					vip_desc: '',
					desc: ''
				},
				addBtnLoading: false
			};
		},
		methods: {
			async loadData() {
				this.formLoading = true;

				await this.$db[__name].tofirst({
					loading: false
				}).then(res => {
					if (res.code == 200) {
						res.datas = res.datas || {};

						this.dataId = res.datas._id || '';

						for (let item in this.form) {
							this.form[item] = res.datas[item];
						}

						if (res.datas.vip_desc) {
							this.form.vip_desc = res.datas.vip_desc;
							this.$refs.editorVipDesc.setContent(this.form.vip_desc);
						}
						if (res.datas.desc) {
							this.form.desc = res.datas.desc;
							this.$refs.editorDesc.setContent(this.form.desc);
						}
					}
					this.formLoading = false;
				});
			},
			submitForm(formName) {
				this.addBtnLoading = true;
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						if (!this.dataId) {
							this.$db[__name].add(this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						} else {
							this.$db[__name].update(this.dataId, this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									return;
									this.addBtnLoading = false;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						}
					}
				});
			},
			editorChangeVipDesc(res) {
				this.form.vip_desc = res;
			},
			editorChangeDesc(res) {
				this.form.desc = res;
			}
		},
		created() {
			this.loadData();
		},
		mounted() {
			this.$nextTick(() => {
				this.$api.scrollOnStorage('.use-box', this.$route.path);
			});

			this.$nextTick(() => {
				this.$api.scrollToStorage('.use-box', this.$route.path);
			});
		}
	};
</script>

<style>
	>>>.input {
		width: 100px;
		margin: 0 10px;
	}

	>>>.input.txt {
		width: 300px;
		margin: 0 10px;
	}

	>>>.input.txt .el-input__inner {
		padding-right: 30px;
	}

	>>>.input .el-input__inner {
		width: 100%;
		background-color: #f6f6f6;
		border-color: #f6f6f6;
		padding: 0 0 0 15px;
	}

	.front_txt {
		width: 200px;
		text-align: right;
		font-size: 13px;
		user-select: none;
	}

	.rear_txt {
		font-size: 13px;
		display: flex;
		align-items: center;
		user-select: none;
	}
</style>
